package com.shire.shire_interface.common.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CMap<K,V> extends HashMap<K,V> implements Map<K,V>{

    public CMap() {
        super();
    }

    public CMap(Map<? extends K, ? extends V> m) {
        super(m);
    }

    public Integer getInt(String key){
        return getInt(key, null);
    }

    public Integer getInt(String key, Integer def){
        Object o = get(key);
        if(o==null){
            return def;
        }
        if(o instanceof Number){
            return ((Number) o).intValue();
        }
        return StrUtils.getIntValue(String.valueOf(o), def);
    }

    public Long getLong(String key){
        return getLong(key, null);
    }

    public Long getLong(String key, Long def){
        Object o = get(key);
        if(o==null){
            return def;
        }
        if(o instanceof Number){
            return ((Number) o).longValue();
        }
        return StrUtils.getLongValue(String.valueOf(o), def);
    }

    public String getString(String key, String def){
        String o = String.valueOf(get(key));
        return o==null ? def:o;
    }

    public CMap<String, Object> getCMap(String key){
        Object o = get(key);
        if(o==null){
            return null;
        }
        if(o instanceof CMap){
            return (CMap<String, Object>)o;
        }
        if(o instanceof Map){
            return new CMap<>((Map<String, Object>)o);
        }
        return null;
    }

    public List<Map<String, Object>> getList(String key){
        Object o = get(key);
        if(o==null) {
            return null;
        }
        if(o instanceof List){
            return (List<Map<String, Object>>) o;
        }
        return null;
    }

    public static CMap<String, Object> getCMap(Map<String, Object> map){
        if(map==null){
            return new CMap<>();
        }
        return new CMap<>(map);
    }

    public static Map<String, Object> responseFailure(Integer status, String msg) {
        Map<String, Object> result = new CMap<String, Object>();
        result.put("success", false);
        result.put("status", status);
        result.put("msg", msg);
        return result;
    }

    public static Map<String, Object> responseFailure() {
        return responseFailure("操作失败");
    }

    public static Map<String, Object> responseFailure(String msg){
        return responseFailure(-1, msg);
    }

    public static Map<String, Object> responseSuccess() {
        return responseSuccess("操作成功");
    }

    public static Map<String, Object> responseSuccess(String msg) {
        Map<String, Object> result = new CMap<String, Object>();
        result.put("success", true);
        result.put("status", 0);
        result.put("msg", msg);
        return result;
    }
}
